﻿using System.Windows;
using Com.Lancetinc.Evs.Config;
using Com.Lancetinc.Evs.Helpers;
using Com.Lancetinc.Evs.Reports;

namespace Com.Lancetinc.Evs.Windows
{
    /// <summary>
    /// 常用术语
    /// </summary>
    public partial class TerminologyEditWindow
    {
        private string _initTerms;
        public TerminologyEditWindow()
        {
            ThemeHelper.ChangeThemeToDark(this);

            InitializeComponent();
        }

        private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            TxtTerms.Text = AppConfigs.ReportConfig.Terms;
            _initTerms = AppConfigs.ReportConfig.Terms;
        }

        private void BtnOk_OnClick(object sender, RoutedEventArgs e)
        {
            if (TxtTerms.Text == _initTerms)
            {
                this.DialogResult = false;
                return;
            }

            if (!ValidTerms())
            {
                return;
            }

            AppConfigs.ReportConfig.Terms = TxtTerms.Text.Trim();
            AppConfigs.ReportConfig.Save();
            this.DialogResult = true;
        }

        private bool ValidTerms()
        {
            var termStr = TxtTerms.Text.Trim();
            if (string.IsNullOrEmpty(termStr))
                return true;

            var lines = termStr.SplitNewLine(true);
            if (lines.Length < 2)
            {
                // 请输入正确的术语
                MessageHelper.ShowErrorByMsgId("TerminologyEditWindow.PlzEnterCorrectTerm");
                return false;
            }

            if (!lines[0].StartsWith(ReportTerminology.GroupPrefix))
            {
                // 术语分组必须以@开头
                MessageHelper.ShowErrorByMsgId("TerminologyEditWindow.WrongGroupName");
                return false;
            }

            return true;
        }
    }
}